/*
 * Copyright (c) 2018. 玺得（深圳）科技有限公司.保留所有权利. http://www.pi-top.com/ 玺得（深圳）科技有限公司保留所有代码著作权.如有任何疑问请访问官方网站与我们联系. 代码只针对特定需求定制编码，不得在未经允许或授权的情况下对外传播扩散.恶意传播者，法律后果自行承担. 本代码仅用于pps-cloud系统.
 */
package com.pitop.setting.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baidu.fsg.uid.UidGenerator;
import com.pitop.core.base.BaseDAO;
import com.pitop.core.base.BaseSVImpl;
import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.exceptions.SettingException;
import com.pitop.setting.dao.SettingDAO;
import com.pitop.setting.entity.Setting;
import com.pitop.setting.entity.SettingState;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("settingSV")
@CacheConfig(cacheNames = "Cloud_Support_Setting_Cache")
@Slf4j
public class SettingSVImpl extends BaseSVImpl<Setting, Long> implements SettingSV {

    @Autowired
    private SettingDAO settingDAO;

    @Resource
    private UidGenerator uidGenerator;

    @Override
    protected BaseDAO getBaseDAO() {
        return settingDAO;
    }

    /**
     * 保存account对象
     *
     * @param entity 实体
     * @throws BaseException
     */
    @Override
//    @CachePut(key = "#entity.id") 没有返回值的方法无法缓存
    @CacheEvict(allEntries = true)//清除所有缓存
    public void save(Setting entity) throws BaseException {
        entity.setVer(1);
        super.save(entity);
    }

    /**
     * 数据更新
     *
     * @param setting
     * @throws BaseException
     */
    @Override
//    @CachePut(key = "#p0.id")
    @CacheEvict(allEntries = true)//清除所有缓存
    public void modify(Setting setting) throws BaseException {
        int oldVer = loadByK(setting.getK()).getVer();
        setting.setVer(oldVer + 1);
        Map<String, Object> paraMap = JSON.parseObject(JSON.toJSONString(setting, SerializerFeature.WriteDateUseDateFormat));
        paraMap.put("oldVer", oldVer);
        getBaseDAO().update(paraMap);
    }


    /**
     * 加载一个对象Setting
     *
     * @param id id* @param k 键
     * @return Setting
     */
    @Override
    @Cacheable(key = "T(String).valueOf(#p0).concat('-').concat(#p1)")
    public Setting load(java.lang.Long id, java.lang.String k) {
        if (id == null && k == null) {
            throw new SettingException(BaseException.BaseExceptionEnum.Illegal_Param);
        }

        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("k", k);
        return settingDAO.load(param);
    }

    /**
     * 加载一个对象Setting 通过id
     *
     * @param id id
     * @return Setting
     */
    @Override
    @Cacheable(key = "T(String).valueOf(#p0)")
    public Setting loadById(java.lang.Long id) {
        if (id == null) {
            throw new SettingException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return settingDAO.loadById(id);
    }

    /**
     * 加载一个对象Setting 通过k
     *
     * @param k 键
     * @return Setting
     */
    @Override
    @Cacheable(key = "#k")
    public Setting loadByK(java.lang.String k) {
        if (k == null) {
            throw new SettingException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return settingDAO.loadByK(k);
    }


    /**
     * 根据主键id,oldStates 共同更新 Setting 的状态到newState状态
     *
     * @param id        id
     * @param newState  新状态
     * @param oldStates 旧状态集合
     */
//    @CachePut(key = "T(String).valueOf(#id)")
    @CacheEvict(allEntries = true)
    public void updateStateById(java.lang.Long id, SettingState newState, SettingState... oldStates) {
        if (id == null) {
            throw new SettingException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (newState == null) {
            throw new SettingException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (oldStates == null) {
            throw new SettingException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        settingDAO.updateStateById(id, newState, oldStates);
    }

    /**
     * 根据主键k,oldStates 共同更新 Setting 的状态到newState状态
     *
     * @param k         键
     * @param newState  新状态
     * @param oldStates 旧状态集合
     */
//    @CachePut(key = "#k")
    @CacheEvict(allEntries = true)
    public void updateStateByK(java.lang.String k, SettingState newState, SettingState... oldStates) {
        if (k == null) {
            throw new SettingException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (newState == null) {
            throw new SettingException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (oldStates == null) {
            throw new SettingException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        settingDAO.updateStateByK(k, newState, oldStates);
    }

    /**
     * 根据主键id 更新 Setting 的状态到另一个状态
     *
     * @param id    id
     * @param state 状态
     */
//    @CachePut(key = "T(String).valueOf(#id)")
    @CacheEvict(allEntries = true)
    public void updateById(java.lang.Long id, SettingState state) {
        if (id == null) {
            throw new SettingException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        settingDAO.updateById(id, state);
    }

    /**
     * 根据主键k 更新 Setting 的状态到另一个状态
     *
     * @param k     键
     * @param state 状态
     */
//    @CachePut(key = "#k")
    @CacheEvict(allEntries = true)
    public void updateByK(java.lang.String k, SettingState state) {
        if (k == null) {
            throw new SettingException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        settingDAO.updateByK(k, state);
    }


    /**
     * 删除对象Setting
     *
     * @param id id* @param k 键
     * @return Setting
     */
    @Override
    @CacheEvict(key = "T(String).valueOf(#p0).concat('-').concat(#p1)")
    public void delete(java.lang.Long id, java.lang.String k) {
        if (id == null && k == null) {
            throw new SettingException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("k", k);
        settingDAO.delete(param);
    }


    /**
     * 查询Setting分页
     *
     * @param setting 对象
     * @param offset  查询开始行
     * @param limit   查询行数
     * @return List<Setting>
     */
    @Override
    @Cacheable(key = "'listSetting'")
    public List<Setting> list(Setting setting, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }

        Map<String, Object> map = null;
        if (setting != null) {
            map = JSON.parseObject(JSON.toJSONString(setting, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashMap<>();
        }
        return settingDAO.list(map, new RowBounds(offset, limit));
    }

    public int count(Setting setting) {
        Map<String, Object> map = null;
        if (setting != null) {
            map = JSON.parseObject(JSON.toJSONString(setting, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashMap<>();
        }
        return settingDAO.count(map);
    }

    /**
     * 查询Setting分页
     *
     * @param id     id* @param k  键
     * @param offset 查询开始行
     * @param limit  查询行数
     * @return List<Setting>
     */
    @Override
    @Cacheable(key = "'listIdOrK'")
    public List<Setting> list(java.lang.Long id, java.lang.String k, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("k", k);
        return settingDAO.list(param, new RowBounds(offset, limit));
    }

    @Override
    public int count(java.lang.Long id, java.lang.String k) {
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("k", k);
        return settingDAO.count(param);
    }

}
